/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import cn.webrx.entity.Book;
import cn.webrx.entity.Student;
import cn.webrx.mapper.BookMapper;
import cn.webrx.mapper.BookVoMapper;
import cn.webrx.mapper.DbMapper;
import cn.webrx.mapper.StudentMapper;
import cn.webrx.mapper.StudentVoMapper;
import cn.webrx.services.BookServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.AES;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p></p>
 * <p>Powered by webrx On 2023-04-25 14:25:43</p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */

@SpringBootTest
@Log4j2
public class BootDemo {

    @Autowired
    ApplicationContext ctx;

    @Autowired
    SqlSession ss;

    @Autowired
    DbMapper dm;

    @Autowired
    BookMapper bm;

    @Autowired
    BookServiceImpl bs;

    @Autowired
    BookVoMapper bvm;

    @Autowired
    StudentMapper sm;

    @Autowired
    StudentVoMapper svm;

    @Test
    void m24(){
        Student s1 = sm.selectById(23);
        s1.setDept("aa");
        System.out.println(s1);
        //
        Student s2 = sm.selectById(23);
        s2.setDept("bb");
        System.out.println(s2);

        sm.updateById(s1);
        sm.updateById(s2);

    }

    @Test
    void m23() {
        bm.selectList(null);
        log.info("hello ok");
        //1.生成密钥
        //String key = AES.generateRandomKey();
        //log.info(key);
        String key = "b5209c77a2e7ec64";

        //2.加密，生成密文
        System.out.println(AES.encrypt("root", key)); //uObZO3utE4jdurjprvefQ==
        System.out.println(AES.encrypt("root", key)); //uObZO3utE4jdurjprvefQ==
        System.out.println(AES.encrypt("jdbc:mysql:/db", key));//CPwkgNeREvTpNvddQ9snIg==

        //3.application.yml 添加 mpw:开头
        // username: mpw:uObZO3utE4jdurjprvefQ==
        // password: mpw:uObZO3utE4jdurjprvefQ==

    }


    @Test
    void m22() {
        //QueryWrapper<User> wrapper = new QueryWrapper<>();
        //wrapper.select("id", "name", "age")
        //        .eq("gender", "male")
        //        .between("age", 20, 30)
        //        .inSql("city", "select city from city_table where province='Guangdong'")
        //        .orderByDesc("register_time");
        //
        //List<User> userList = userMapper.selectList(wrapper);

        QueryChainWrapper<Book> w = new QueryChainWrapper<>(bm);
        List<Book> list = w.select("id", "name").lt("price", 25).list();
        System.out.println(list);
    }


    @Test
    void m21() {
        //0当前页 2每页多少条记录
        Page<Book> p = new Page<>(23, 2);

        //Page<Book> p = new MyPage<>(5,2);

        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        p = bm.selectPage(p, wrapper);

        //判断当前页，如果超出总页，重新分页，设定当前页为最后一页
        if (p.getCurrent() > p.getPages()) {
            p = bm.selectPage(new Page<>(p.getPages(), p.getSize()), wrapper);
        }


        System.out.println(p.getRecords());
        System.out.println("---------------------");
        System.out.println(p.getCurrent());
        System.out.println(p.getPages());
        System.out.println(p.getSize());
        System.out.println(p.getTotal());
        System.out.println(p.getRecords());
    }

    @Test
    void m20() {
        //bm.selectList(null); 返回所有List<Book>
        //bm.selectById(1651037821214986241L); 返回指定的id的Book实例
        //System.out.println(bm.selectById(10L));//如果指定的id不存在，则返回null空对象

        QueryWrapper<Book> w = new QueryWrapper<>();
        w.select("id", "name");
        w.gt("price", 15);
        w.orderByAsc("id").orderByDesc("name"); //ORDER BY id ASC,name DESC查询结果排序
        //List<Book> list = bm.selectList(w); //SELECT id,name FROM t_book WHERE (price > ?)
        //System.out.println(list);
        //Book(id=1651037632836186113, name=《java入门实战》, price=null, author=null, pdate=null)

        //SELECT id,name,price,author,pdate FROM t_book WHERE id IN ( ? , ? , ? )
        //System.out.println(bm.selectBatchIds(List.of(1, 2, 3)));

        w.clear();
        w.eq("price", 35); //SELECT COUNT( * ) AS total FROM t_book WHERE (price = ?)

        //Long aLong = bm.selectCount(w); //SELECT COUNT( * ) AS total FROM t_book
        //System.out.println(aLong);

        w.clear();
        w.select("id", "name", "price");

        //selectMaps() 方法返回一个List<Map<String,Object>)
        //List<Map<String, Object>> maps = bm.selectMaps(w);
        //System.out.println(maps);

        w.clear();
        //System.out.println(bm.selectObjs(w).get().getClass().getSimpleName());
        w.eq("price", 66d);
        //Book book = bm.selectOne(w); //SELECT id,name,price,author,pdate FROM t_book WHERE (price = ?)
        //System.out.println(book);


        //System.out.println(bs.getById(1651038107501359106L));


    }

    @Test
    void m19() {
        //svm.selectList(null);
        //sm.selectList(null);
        sm.queryAll();
    }


    @Test
    void m18() {
        Student st = new Student();
        st.setId(1000);
        st.setName("王二强");
        st.setGender("男");
        st.setBirth(LocalDate.now());
        st.setScore(90);
        st.setMoney(10.0D);
        st.setDept("会计");
        st.setAddress("河南开封");
        sm.insert(st);

        //System.out.println(sm.selectList(null));

        //SELECT id,name,gender,birth,score,money,dept,address,isdelete FROM t_student WHERE id=? AND isdelete=0
        //Student st = sm.selectById(23);
        //System.out.println(st); //null

        //sm.deleteById(23); UPDATE t_student SET isdelete=1 WHERE id=? AND isdelete=0
    }

    @Test
    void m17() {
        //mapper 插入数据
        Book book = new Book();
        book.setName("《vue3》");
        book.setPrice(11d);
        book.setAuthor("jack");
        book.setPdate(LocalDate.now());
        //bm.insert(book);
        //bs.save(book);
        //bs.saveOrUpdate(book);

        //Book bk = bm.selectById(1651038107501359106L);
        //bk.setName("《vue3入门》");
        //一个是插入insert语句，如果对象有id，会根据id修改数据update语句
        //bs.saveOrUpdate(bk);

        //所有数据价格上调5元 update t_book set price = price+5;
        UpdateWrapper<Book> w = new UpdateWrapper<>();
        Book bks = new Book();
        w.setSql("price = price + 5"); //UPDATE t_book SET price = price + 5
        w.le("price", 30);  //UPDATE t_book SET price = price + 5 WHERE (price <= ?)
        //bm.update(null,w);
        //bm.update(new Book(),w);

        w.clear();
        //w.set(true,"abc",500); //UPDATE t_book SET abc=?
        w.set("a", "aaaa");
        w.set("b", "baaa");
        w.set("c", "caaa");
        w.set("d", "daaa");
        w.like("name", "aaa");  //Preparing: UPDATE t_book SET a=?,b=?,c=?,d=? WHERE (name LIKE ?)
        w.clear();

        w.in(true, "id", 1651037821214986241L, 1651038107501359106L);
        w.setSql(true, "price=price+2"); //UPDATE t_book SET price=price+2 WHERE (id IN (?,?))
        //bm.update(null,w);


        Book t = bm.selectById(1651038107501359106L);
        t.setPrice(66D);
        //bs.saveOrUpdate(t);  先查询select，发布有，语句直接变为update

        bs.updateById(t); //UPDATE t_book SET name=?, price=?, author=?, pdate=? WHERE id=?
    }


    @Test
    @DisplayName("mp delete")
    void m16() {
        //无条件清空表删除所有
        //bm.delete(null);
        //bm.deleteById(3);
        //Book book = new Book();
        //book.setId(15L);
        //bm.deleteById(book);

        //根据id集合批量删除 delete from t where id in ()
        //bm.deleteBatchIds(List.of(1,2,3,4,5));
        //bm.deleteBatchIds(List.of("uuid1","uuid2","uuid3"));

        Map<String, Object> map = new HashMap<>();
        map.put("id", 6);
        map.put("name", "java");
        map.put("price", 50);
        //DELETE FROM t_book WHERE price = ? AND name = ? AND id = ?
        //bm.deleteByMap(map);

        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.gt(true, "price", 50);
        wrapper.le(true, "price", 15);
        wrapper.or().le(true, "price", 15);
        //wrapper.clear();
        //bm.delete(wrapper);
    }

    @Test
    void m15() {
        //System.out.println(bm.selectList(null));
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        //wrapper.like(true,"name","java");
        //wrapper.like(false,"name","mp");
        //wrapper.likeLeft(false,"name","mp");
        //wrapper.likeRight("name","mp");
        //wrapper.likeRight(true,"name","mp");

        //"author"是数据表的列名
        //wrapper.likeRight("author","李");
        wrapper.gt(true, "price", 10);

        System.out.println(bm.selectList(wrapper));
    }

    @Test
    void m14() {
        System.out.println(bvm.selectList(null));
        System.out.println(bm.queryAll());
    }


    @Test
    void m13() {
        Book book = new Book();
        book.setName("《mp开发入门》");
        book.setPrice(50d);
        //book.setAuthor("李四");
        book.setPdate(LocalDate.now());
        bs.save(book);

        //bm.insert(book);

        //aaa396923d722e423bc467c54d59c051
        System.out.println("aaa396923d722e423bc467c54d59c051".length());
    }

    @Test
    void m12() {
        List<Book> books = bm.selectList(null);
        System.out.println(books);

        Book b = bm.selectById(1033);
        System.out.println(b);

        System.out.println(bm.shows());

        System.out.println(bm.count());

    }

    @Test
    void m11() {
        System.out.println(dm.ver());
        System.out.println(dm.dbs());
        System.out.println(dm.tbs());
        System.out.println(dm.tbs("erp"));
    }


    @Test
    void m10() {
        System.out.println(ss.selectList("dbs"));
        System.out.println(ss.selectList("tbs"));
        System.out.println(ss.selectList("tbs", "mysql"));
    }


    @Test
    void m() {
        System.out.println(ctx.getBeanDefinitionCount());
        System.out.println("hello");
    }
}
